﻿using System;
using NUnit.Framework;
using NetVoxLab.Framework;
using NetVoxLab.Framework.Model;
using Version = NetVoxLab.Framework.Model.Version;

namespace NetVoxLab.Tests.Framework
{  
    [TestFixture]
    public class UtilitiesTests
    {
        private TypeA _typeA;

        [SetUp]
        public void SetUp()
        {
            _typeA = new TypeA
                         {
                             Name = "TypeAName",
                             Capacity = 34,
                             Version = new Version(1,456)
                         };
        }

        [Test]
        public void GetPropValue_TypeA_StringValue()
        {
            var name = (string)Utilities.GetPropValue("name", _typeA);

            Assert.That(name, Is.EqualTo("TypeAName"));
        }

        [Test]
        public void GetPropValue_TypeACapacity_ValueType()
        {
            var capacity = (int) Utilities.GetPropValue("capacity", _typeA);
            Assert.That(capacity, Is.EqualTo(34));
        }

        [Test]
        public void GetPropValue_TypeAVersion_ReferenceType()
        {
            var version = (Version) Utilities.GetPropValue("version", _typeA);
            Assert.That(version.Integral, Is.EqualTo(1));
            Assert.That(version.Fractional, Is.EqualTo(456));
        }

        [Test]
        [ExpectedException(typeof(NullReferenceException))]
        public void GetPropValue_WrongPropertyName_ThrowNullReferenceException()
        {
            var id = (int) Utilities.GetPropValue("id", _typeA);
            Assert.That(id, Is.EqualTo(0));
        }

        [Test]
        public void GetPropValue_WrongProperty_Null()
        {
            var prop = Utilities.GetPropValue("notExsistedProperty", _typeA);
            Assert.That(prop, Is.Null);
        }

        [Test]
        public void GetPropValue_EmptyPropertyProperty_Null()
        {
            var prop = Utilities.GetPropValue("", _typeA);
            Assert.That(prop, Is.Null);
        }

        [Test]
        public void GetPropValue_NullObject_Null()
        {           
            var prop = Utilities.GetPropValue("name", null);
            Assert.That(prop, Is.Null);
        }

        [Test]
        public void GetPropValue_AttachedBlobGetName_Null()
        {
            var blob = new AttachedBlob {Value = new byte[] {3, 42, 32}};
            var name = (string) Utilities.GetPropValue("name", blob);

            Assert.That(name, Is.Null);
        }
    }
}
